package com.yeyks.app.controller.base.consumer;

import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.dto.FishBallGoodsDTO;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.commonReference.pojo.param.consumer.FishBallGoodsConversionParam;
import com.yeyks.commonReference.service.consumer.FishBallGoodsService;
import com.yeyks.consumer.dal.domain.FishBallGoods;
import com.yeyks.consumer.dal.domain.UserInfo;
import com.yeyks.consumer.dto.SignInDTO;
import com.yeyks.consumer.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author fuchangshuai
 * @date 2019/4/24 0024
 */
@Api(tags = "商品券接口", description = "商品券")
@Slf4j
@RestController
@RequestMapping(value = "/app/ball/goods")
public class FishBallGoodsController {
    @Autowired
    private FishBallGoodsService fishBallGoodsService;
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 兑换商品券
     *
     * @param token
     * @return
     */
    @ApiOperation(value = "兑换商品券", notes = "兑换商品券(兑换成功true, 失败false)")
    @PostMapping(value = "/conversion")
    public AjaxResponseBody<SignInDTO> conversion(@Validated FishBallGoodsConversionParam param) {
        UserInfo userInfo = userInfoService.tokenUserInfo(param.getToken());
        Integer remainAmount = fishBallGoodsService.conversion(userInfo, param);
        if (remainAmount == -1) {
            throw new ServiceException("兑换商品失败");
        }
        SignInDTO signInDTO = new SignInDTO();
        signInDTO.setRemainAmount(remainAmount);
        return AjaxResponseBody.success(signInDTO);
    }

    /**
     * 获取商品券列表
     *
     * @return
     */
    @ApiOperation(value = "获取商品券列表", notes = "获取商品券列表", response = FishBallGoodsDTO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNo", value = "当前页", defaultValue = "1", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "页码", defaultValue = "10", dataType = "int")
    })
    @PostMapping(value = "/list")
    public AjaxResponseBody<List<FishBallGoodsDTO>> list(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        List<FishBallGoodsDTO> fishBallGoodsDTOS = fishBallGoodsService.list(pageNo, pageSize);
        return AjaxResponseBody.success(fishBallGoodsDTOS);
    }

    @ApiOperation(value = "获取商品信息", notes = "获取商品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", defaultValue = "10", dataType = "int")
    })
    @GetMapping(value = "/get")
    public AjaxResponseBody<FishBallGoods> get(@RequestParam(name = "id", defaultValue = "10") Integer id) {
        if (Objects.isNull(id)) {
            new ServiceException("id不能为空");
        }
        FishBallGoods fishBallGoods = fishBallGoodsService.getById(id);
        return AjaxResponseBody.success(fishBallGoods);
    }

}
